package com.liyi.letter.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liyi.letter.common.result.Result;
import com.liyi.letter.system.entity.Configuration;
import com.liyi.letter.system.entity.Department;
import com.liyi.letter.system.service.ConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/configuration/system")
public class ConfigurationController {
    @Autowired
    private ConfigurationService configurationService;


    /**
     * 对配置进行添加操作
     * @param configuration
     * @return
     */
    @PostMapping("/add")
    public Result<Configuration> add(@RequestBody Configuration configuration){
        boolean success = configurationService.save(configuration);
        if (success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    /**
     * 根据id修改配置
     * @param configuration
     * @return
     */
    @PostMapping("/update")
    public Result<Configuration> update(@RequestBody Configuration configuration){
        boolean success = configurationService.updateById(configuration);
        if (success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据id对配置进行删除操作
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Result<Configuration> delete(@PathVariable String id){
        boolean success = configurationService.removeById(id);
        if (success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据名字进行分页查找
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page/{page}/{pageSize}")
    public Result page(@PathVariable Integer page, @PathVariable Integer pageSize,String name){
        Page<Configuration> configurations = configurationService.page(page, pageSize, name);
        return Result.ok(configurations);
    }


    /**
     * 查找所有
     * @return
     */
    @GetMapping("findAll")
    public Result<List> findAll(){
        List<Configuration> list = configurationService.list();
        return Result.ok(list);
    }
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable("id") Long id){
        Configuration configuration = configurationService.getById(id);
        return Result.ok(configuration);

    }
}
